package cn.fayang.system.controller;

import cn.fayang.basic.util.AjaxResult;
import cn.fayang.basic.util.PageList;
import cn.fayang.system.domain.Detail;
import cn.fayang.system.query.DetailQuery;
import cn.fayang.system.service.IDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制器
 *
 */
@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailService detailService;

    /**
     * 通过ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Detail queryById(@PathVariable("id") Long id){
        return detailService.queryById(id);

    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<Detail> qureyAll(){
        return detailService.queryAll();
    }

    /**
     * 分页查询
     * @param detailQuery
     * @return
     */
    @PostMapping
    public PageList<Detail> querPage(@RequestBody DetailQuery detailQuery){
        System.out.println(detailQuery);
        return  detailService.queryPage(detailQuery);
    }

    /**
     * 删除
     * @param id
     * @return
     */

    @DeleteMapping("{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            detailService.deleteByID(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }
    @PatchMapping
    public AjaxResult Batchremove(@RequestBody List<Long> ids){
        try {
            detailService.batchDelete(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }

    /**
     * 增加和修改
     * @param detail
     * @return
     */
    @PutMapping
    public  AjaxResult addOrUpdate(@RequestBody Detail detail){
        try {
            if(detail.getId() == null){
                detailService.save(detail);
            }else {
                detailService.update(detail);
            }
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后再试");
        }
    }





}
